//
//  ACHCommon.m
//  MansApp
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "ACHCommon.h"
#import "SportModel.h"
#import "ScienceTechnologyModel.h"//科技频道
#import "CompositModel.h"
<<<<<<< HEAD
@interface ACHCommon ()

@end


@interface ACHCommon ()

@end

=======
>>>>>>> ce07ad031b273e163ee5baee78bde31b78b9f7b4
#import "CarModel.h"
@interface ACHCommon ()

@end

@implementation ACHCommon

+ (NSMutableArray *)spotrModelAsynchronousURL:(NSString *)url collectionView:(UICollectionView*)collectionView{
    
    if (!url) {
        return nil;
    }
    
    NSMutableArray *tempArray = [NSMutableArray array];
    __block NSMutableArray *myArr = tempArray;
    
    [ACHNetWork GET:url success:^(id response) {
        NSDictionary *dict = response;
        NSDictionary *dict1 = dict[@"data"];
        NSArray *array = dict1[@"articles"];
        
        
        NSMutableArray *paixu = [NSMutableArray array];
        
        NSDictionary *ipadconfig = dict1[@"ipadconfig"];
        NSArray *pages = ipadconfig[@"pages"];
        
        for ( int i = 0; i < pages.count; i++) {
            
            NSString *str = [pages[i] objectForKey:@"articles"];
            [paixu addObject:str];
            NSMutableArray *arr = [NSMutableArray array];
            
            [myArr addObject:arr];
        }
        
        for (NSDictionary * obj in array) {
            SportModel *model = [[SportModel alloc]init];

            [model modelWithDictionary:obj];
            [model modelWithDictionary:dict1[@"info"]];
            NSArray *picArray = obj[@"media"];
            
            model.pk = obj[@"pk"];
            if (picArray.count != 0) {
                NSDictionary *picDict = picArray.firstObject;
                model.url = picDict[@"url"];
                NSDictionary *aDict = pages.firstObject;
                NSDictionary *diy = aDict[@"diy"];
     
               [model modelWithDictionary:diy];
    
                
              
                for (int i = 0 ; i < paixu.count; i++) {
                    if ([paixu[i] containsString:model.pk]) {
                        [myArr[i]addObject:model];
                        break;
                    }
                    
                }
                
                [collectionView reloadData];
                
                
                
                
            }
            else{
                [ACHNetWork GET:model.full_url success:^(id response) {
                    NSDictionary *dictN = response[@"data"];
                    NSArray *arrayN = dictN[@"media"];
                    if (arrayN.count != 0) {
                        model.url =  arrayN.firstObject[@"url"];
                    }
                    
                    NSDictionary *aDict = pages.firstObject;
                    NSDictionary *diy = aDict[@"diy"];
                    
                    [model modelWithDictionary:diy];
                    
                    for (int i = 0 ; i < paixu.count; i++) {
                        if ([paixu[i] containsString:model.pk]) {
                            [myArr[i]addObject:model];
                            break;
                        }
                        
                    }
                
                    [collectionView reloadData];
                    
                } failure:nil];
                
                
            }
            
        };
        
        
        
    } failure:nil];
    
    
    return  tempArray;
    
}

//科学频道
<<<<<<< HEAD

+ (NSMutableArray *)scienceModelAsynchronousURL:(NSString *)url collectionVIew:(UICollectionView *)collectionView

=======
<<<<<<< HEAD

+ (NSMutableArray *)scienceModelAsynchronousURL:(NSString *)url collectionVIew:(UICollectionView *)collectionView

=======
+ (NSMutableArray *)scienceModelAsynchronousURL:(NSString *)url collectionVIew:(UICollectionView *)collectionView
>>>>>>> 18f0b82748c4fe05fe44650cf544b189a96cf31b
>>>>>>> ce07ad031b273e163ee5baee78bde31b78b9f7b4
{
    if (!url) {
        return nil;
    }
    NSMutableArray *tempArray = [NSMutableArray array];
    __block NSMutableArray *myArr = tempArray;
    [ACHNetWork GET:url success:^(id response) {

        NSDictionary *maxDic = response;
        NSDictionary *minDic = [maxDic objectForKey:@"data"];
        NSArray *maxArr = [minDic objectForKey:@"articles"];
        NSDictionary *nextUrlDic = [minDic objectForKey:@"info"];
        
        NSMutableArray *modelArr = [NSMutableArray array];
        for (int i = 0; i<maxArr.count; i++) {
            NSDictionary *obj = maxArr[i];
            ScienceTechnologyModel *model = [ScienceTechnologyModel modelWithDictionary:obj];
            
            model.next_url = [nextUrlDic objectForKey:@"next_url"];
            NSArray *imageArr = [obj objectForKey:@"media"];
            NSDictionary *dic = [obj objectForKey:@"special_info"];
            model.web_url = [dic objectForKey:@"web_url"];
            if (imageArr.count!=0) {
                NSDictionary *imageDic = imageArr[0];
                model.url = [imageDic objectForKey:@"url"];
            }
            [modelArr addObject:model];
            if ((i+1)%6==0) {
                [myArr addObject:modelArr];
                modelArr = [NSMutableArray array];
            }
            [collectionView reloadData];
        }
    } failure:nil];
    
    return tempArray;
}

<<<<<<< HEAD
=======
<<<<<<< HEAD

=======
>>>>>>> 18f0b82748c4fe05fe44650cf544b189a96cf31b
>>>>>>> ce07ad031b273e163ee5baee78bde31b78b9f7b4
// 汽车的解析
+ (NSMutableArray *)carModelAsynchronousURL:(NSString *)url collectionVIew:(UICollectionView *)collectionView{
    
    
    if (!url) {
        return nil;
    }
    
    NSMutableArray *tempArray = [NSMutableArray array];
    __block NSMutableArray *myArr = tempArray;
   [ACHNetWork GET:kCarPicURL success:^(id response) {
       NSDictionary *dict = response;
       NSDictionary *dict1 = dict[@"data"];
       NSString *str = [dict1[@"info"] objectForKey:@"next_url"];

       NSArray *array = dict1[@"articles"];
       
       
       NSMutableArray *paixu = [NSMutableArray array];
       
       NSDictionary *ipadconfig = dict1[@"ipadconfig"];
       NSArray *pages = ipadconfig[@"pages"];

       
       for ( int i = 0; i < pages.count; i++) {
            NSString *str = [pages[i] objectForKey:@"articles"];
           [paixu addObject:str];
           NSMutableArray *arr = [NSMutableArray array];
           
           [myArr addObject:arr];
       }
       
       
       
       for (NSDictionary *obj in array) {
           CarModel *model = [[CarModel alloc]init];
           model.next_url = str;
           
           model.pk = obj[@"pk"];
           NSArray *media = obj[@"media"];
           model.array = [NSMutableArray array];
           for ( NSDictionary *dicc in media) {
               NSString *url = dicc[@"url"];
               
               [model.array addObject:url];
           }
            for (int i = 0 ; i < paixu.count; i++) {
           if ([paixu[i] containsString:model.pk]) {
               
               
             [myArr[i]addObject:model];
               
               
               break;
           }
           
       }
           
           [collectionView reloadData];
           
       }
    
       for (int i = 0; i < myArr.count; i++) {
           
           NSLog(@"~~~~~~ %ld",[myArr[i] count]);
       }

       
       
      
   } failure:nil];
    
    return tempArray;
<<<<<<< HEAD

=======
<<<<<<< HEAD
=======
>>>>>>> ce07ad031b273e163ee5baee78bde31b78b9f7b4
    
>>>>>>> 18f0b82748c4fe05fe44650cf544b189a96cf31b
}


//美图解析
+ (NSMutableArray *)beautifulPictureModelAsynchronousurl:(NSString *)url collectionView:(iCarousel *)collectionView
{
    if (!url) {
        return nil;
    }
    NSMutableArray *tempArray = [NSMutableArray array];
    __block NSMutableArray *myArr = tempArray;
    [ACHNetWork GET:url success:^(id response){
        NSDictionary *maxdic = (NSDictionary *)response;
        NSDictionary *minDic = [maxdic objectForKey:@"data"];
        NSDictionary *smallDic = [minDic objectForKey:@"info"];
        NSArray *articlesArr = [minDic objectForKey:@"articles"];
        for (NSDictionary *dic in articlesArr) {
            AmusementModel *model = [AmusementModel modelWithDictionary:dic];
            model.next_url = [smallDic objectForKey:@"next_url"];
            NSArray *mediaArr = [dic objectForKey:@"media"];
            model.media = [NSMutableArray array];
            for (NSDictionary *imageDic in mediaArr) {
                AmusementModel *imageModel = [[AmusementModel alloc] init];
                imageModel.url = [imageDic objectForKey:@"url"];
                imageModel.m_url = [imageDic objectForKey:@"m_url"];
                imageModel.raw_url = [imageDic objectForKey:@"raw_url"];
                [model.media addObject:imageModel];
            }
            [myArr addObject:model];
            [collectionView reloadData];
        }
    } failure:nil];
    return tempArray;
}


<<<<<<< HEAD
=======
<<<<<<< HEAD



=======
>>>>>>> 18f0b82748c4fe05fe44650cf544b189a96cf31b
>>>>>>> ce07ad031b273e163ee5baee78bde31b78b9f7b4
@end
